/*
 * ConfigurationMannagerPool.cpp
 *
 *      Author: carlos
 */

#include "ConfigurationMannagerPool.h"
#include "../utils/const.h"
#include "../utils/StringUtils.h"
#include "../exceptions/ProgramException.h"

STRING2CONFIGURATIONMANAGER ConfigurationMannagerPool::instances;
ConfigurationMannagerPool::ConfigurationMannagerPool() {

}
ConfigurationMananger* ConfigurationMannagerPool::getInstance(
		ESTRUCTURAS estuctura) {
	string name = ConfigurationMannagerPool::getStructureName(estuctura);

	return ConfigurationMannagerPool::getInstance(name);
}
string ConfigurationMannagerPool::getStructureName(ESTRUCTURAS estuctura) {
	string name = "";

	switch (estuctura) {
	case ELEMENT:
		name="ELEMENT.";
		break;
	case RSS:
		name="RSS.";
		break;
	case TWEET:
		name="TWEET.";
		break;
	case INDEX_TEST:
		name = "INDEX_TEST.";
		break;
	case FECHA_IDRSS:
		name = "FECHA_IDRSS.";
		break;
	case FECHA_IDTWEET:
		name = "FECHA_IDTWEET.";
		break;
	case AUTOR_IDRSS:
		name = "AUTOR_IDRSS.";
		break;
	case AUTOR_IDTWEET:
		name = "AUTOR_IDTWEET.";
		break;
	case FECHADES_IDRSS:
		name = "FECHADES_IDRSS.";
		break;
	case FECHADES_IDTWEET:
		name = "FECHADES_IDTWEET.";
		break;
	case FECHAASC_IDRSS:
		name = "FECHAASC_IDRSS.";
		break;
	case FECHAASC_IDTWEET:
		name="FECHAASC_IDTWEET.";
		break;
	case TITULO_IDRSS:
		name="TITULO_IDRSS.";
		break;
	case SIMPLE_SECONDARY_INDEX:
		name="SIMPLE_SECONDARY_INDEX.";
		break;
	default:
		name = "";
		string mensaje;
		mensaje.append("no se configuro el configuration mannager para ");
		mensaje.append(StringUtils::convertIntToString(estuctura));
		throw ProgramException(mensaje);
		break;
	}

	return name;
}

ConfigurationMananger* ConfigurationMannagerPool::getInstance(string name) {
	STRING2CONFIGURATIONMANAGER::iterator mapIterator;
	mapIterator = ConfigurationMannagerPool::instances.find(name);
	if (mapIterator != ConfigurationMannagerPool::instances.end()) {
		return (*mapIterator).second;
	} else {
		ConfigurationMananger* configMananger = new ConfigurationMananger(name);
		ConfigurationMannagerPool::instances.insert(
				STRING2CONFIGURATIONMANAGER::value_type(name, configMananger));
		return configMananger;
	}
}
ConfigurationMannagerPool::~ConfigurationMannagerPool() {
	//TODO eliminar de la memoria todas las instancias
}
